<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item prop="name" label="讲师名称">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" min="0" :max="200000000" controls-position="right" :min="0"/>
      </el-form-item>
      <el-form-item prop="level" label="讲师头衔">
        <el-select v-model="teacher.level" clearable placeholder="请选择">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item prop="career" label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item prop="intro" label="讲师简介">
        <el-input v-model="teacher.intro" :rows="8" type="textarea"/>
      </el-form-item>

      <!-- 讲师头像：TODO -->
      <el-form-item label="讲师头像">
        <div style="margin-left: 100px" class="components-container">
          <!--          缩略图-->
          <pan-thumb :image="teacher.avatar">
            英俊帅气的脸庞!
          </pan-thumb>

          <el-button type="primary" icon="upload" style="margin-left: 50px;"
                     @click="imagecropperShow=true">点我上传/修改图片
          </el-button>

          <image-cropper
            v-show="imagecropperShow"
            :width="300"
            :height="300"
            :key="imagecropperKey"
            :url="BASE_API+'/eduoss/fileoss'"
            field="file"
            lang-type="zh"
            @close="close"
            @crop-upload-success="cropSuccess"/>
          <!--          点击确定的回调-->
        </div>
      </el-form-item>
      <!--      讲师头像结束-->
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
  // 引入组件
  import ImageCropper from '@/components/ImageCropper'
  import PanThumb from '@/components/PanThumb'
  // 引入方法
  import { addTeacher, editTeacher, updateTeacher } from '@/api/edu/teacher'

  export default {
    name: 'AvatarUploadDemo',
    components: { ImageCropper, PanThumb },
    data() {
      return {
        teacher: {
          name: '',
          sort: '',
          level: '',
          career: '',
          intro: '',
          avatar: ''
        },
        panInfo: '000',
        //保存按钮是否禁用 防止表单重复提交
        saveBtnDisabled: false,
        //是否显示上传窗口
        imagecropperShow: false,
        imagecropperKey: 0,
        //显示图片地址
        // image: 'https://wpimg.wallstcn.com/577965b9-bb9e-4e02-9f0c-095b41417191',
        //  获取config下 dev中的地址
        BASE_API: process.env.BASE_API
      }

    },

    created() {
      // console.log((this.$route.params && this.$route.params.id))
      //此处判断路径中是否携带值
      //如果携带 if中为true 否则为undefined
      this.init()
    },
    //监听路由变化
    watch: {
      $route(from, to) {
        this.init()
      }
    },
    methods: {
      //data 是封装过的 相当于response对象
      cropSuccess(data) {
        //准备下一张
        this.imagecropperKey++
        this.imagecropperShow = false
        this.teacher.avatar = data.url
      },
      //关闭方法
      close() {
        //准备下一张
        this.imagecropperKey++
        this.imagecropperShow = false
      },
      //数据回显
      init() {
        if (this.$route.params && this.$route.params.id) {
          this.title = '讲师修改'
          editTeacher(this.$route.params.id).then(response => {
            this.teacher = response.data.teacher
          })

        } else {
          this.teacher = {}
          //此处有bug
          this.title = '讲师添加'
        }
      },
      //保存或修改
      saveOrUpdate() {

        if (this.$route.params && this.$route.params.id) {
          updateTeacher(this.teacher).then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.$router.push({ path: '/teacher/list' })
            console.log(response)
          }).catch(error => {
            console.log(error)

          })
        } else {
          this.saveTeacher()
        }
      },
      // 保存
      saveTeacher() {
        addTeacher(this.teacher).then(response => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          // 路由跳转
          this.$router.push({ path: '/teacher/list' })
        })
      }
    }
  }
</script>
<style scoped>
  .avatar {
    width: 200px;
    height: 200px;
    border-radius: 50%;
  }
</style>
